package com.sdry.approval.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdry.approval.constant.ApprovalTypeEnum;
import com.sdry.approval.entity.ForMySponsor;
import com.sdry.approval.entity.LeaveMan;
import com.sdry.approval.entity.MeSponsor;
import com.sdry.approval.service.ForMySponsorService;
import com.sdry.approval.service.LeaveRequestService;
import com.sdry.approval.service.MeSponsorService;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.UserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @program: info_sdry
 * @description: 我发起的
 * @author: cb
 * @create: 2020-02-24 08:10
 **/
@Controller
@RequestMapping(value = "meSponsor")
public class MeSponsorController extends BaseController {
    @Autowired
    private MeSponsorService meSponsorService;

    @Autowired
    private UserService userService;

    @Autowired
    private ForMySponsorService forMySponsorService;

    @Autowired
    private LeaveRequestService leaveRequestService;

    /**
     * 分页查询
     * @param meSponsor
     * @return
     */
    @RequestMapping(value = "queryMeSponsorPage")
    @ResponseBody
    public TableDataInfo queryMeSponsorPage(MeSponsor meSponsor, HttpServletRequest request) {
        logger.info("获取我发起的列表 sysDept = " + JSONObject.fromObject(meSponsor));
        Page<MeSponsor> meSponsorPage = new Page<>(meSponsor.getPage(),meSponsor.getLimit());
        QueryWrapper<MeSponsor> queryWrapper = new QueryWrapper<>();
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        if (StringUtils.isNotEmpty(meSponsor.getMeSponsorType())){
            queryWrapper.eq("me_sponsor_type",meSponsor.getMeSponsorType()).eq("user_id",user.getUserId());
        }else {
            queryWrapper.eq("user_id",user.getUserId());
        }
        IPage<MeSponsor> meSponsorIPage = meSponsorService.page(meSponsorPage, queryWrapper);
        List<MeSponsor> meSponsors = meSponsorIPage.getRecords();
        for (MeSponsor meSponsor1 : meSponsors) {
            meSponsor1.setRealName(userService.getById(meSponsor1.getUserId()).getRealName());
            String completeStatus = meSponsor1.getCompleteStatus();
            String trim = meSponsor1.getCompleteStatus().trim();
            System.out.println(trim.length());
            try {
                System.out.println(ApprovalTypeEnum.valueOf(trim).getName());
            }catch (Exception e){
            }
            meSponsor1.setCompleteStatus(meSponsor1.getCompleteStatus().trim());
            System.out.println(completeStatus.length());
            //meSponsor1.setMeSponsorType(ApprovalTypeEnum.valueOf(meSponsor1.getMeSponsorType().trim()).getName());
        }
        return getDataTable(meSponsors, meSponsorIPage.getTotal());
    }

    /**
     * 添加
     * @param meSponsorId ID
     * @return boolean
     */
    @RequestMapping(value = "addForMySponsor")
    @ResponseBody
    public boolean addForMySponsor(Long meSponsorId) {
        MeSponsor meSponsor = meSponsorService.getById(meSponsorId);
        boolean boo = false;
        List<LeaveMan> leaveManList =  leaveRequestService.queryLeaveManList(meSponsor.getRequestId(),meSponsor.getMeSponsorType().trim());
        for (LeaveMan leaveMan : leaveManList) {
            ForMySponsor forMySponsor = new ForMySponsor();
            forMySponsor.setCompleteStatus("0");
            forMySponsor.setRequestType(leaveMan.getRequestType());
            forMySponsor.setUserId(leaveMan.getUserId());
            forMySponsor.setRequestId(meSponsor.getRequestId());
            boo = forMySponsorService.save(forMySponsor);
        }
        return boo;
    }


    /**
     * 分页查询
     * @param meSponsor
     * @return
     */
    @RequestMapping(value = "queryCopySponsorPage")
    @ResponseBody
    public TableDataInfo queryCopySponsorPage(MeSponsor meSponsor, HttpServletRequest request) {
        logger.info("获取抄送列表 sysDept = " + JSONObject.fromObject(meSponsor));
        //获取抄送人

        List<MeSponsor> meSponsorList = meSponsorService.queryCopySponsorPage();
        for (MeSponsor meSponsor1 : meSponsorList) {
            meSponsor1.setRealName(userService.getById(meSponsor1.getUserId()).getRealName());
            String trim = meSponsor1.getCompleteStatus().trim();
            try {
                System.out.println(ApprovalTypeEnum.valueOf(trim).getName());
            }catch (Exception e){
            }
            meSponsor1.setCompleteStatus(meSponsor1.getCompleteStatus().trim());
        }

        return getDataTable(meSponsorList,10L);
    }
}
